<?php
/**
 ***** 扶摇而上、抱守本真 ******
 * @author Dana
 * @Date 2021/7/5
 */

class MyCircularQueue
{
    protected $maxSize;
    protected $head;
    protected $tail;
    protected $array;

    /**
     * @param Integer $maxSize
     */
    function __construct($maxSize)
    {
        $this->maxSize = $maxSize;
        $this->head = -1;
        $this->tail = -1;
        $this->array = [];
    }

    /**
     * @param Integer $value
     * @return Boolean
     */
    function enQueue($value = null)
    {
        if ($this->isFull()) {
            return false;
        }
        if($this->isEmpty()){
            $this->head = 0;
        }

        $this->tail = ($this->tail +1) % $this->maxSize;
        $this->array[$this->tail] = $value;
        return true;
    }

    /**
     * @return Boolean
     */
    function deQueue()
    {
        if ($this->isEmpty()) {
            return false;
        }
        if($this->head == $this->tail){
            $this->head = -1;
            $this->tail = -1;
            return true;
        }
        $this->head = ($this->head + 1) % $this->maxSize;
        return true;
    }

    /**
     * @return Integer
     */
    function Front()
    {
        if ($this->isEmpty()) {
            return -1;
        }

        return $this->array[$this->head];
    }

    /**
     * @return Integer
     */
    function Rear()
    {
        if ($this->isEmpty()) {
            return -1;
        }

        return $this->array[$this->tail];
    }

    /**
     * @return Boolean
     */
    function isEmpty()
    {
        return $this->head == -1;
    }

    /**
     * @return Boolean
     */
    function isFull()
    {
       return (($this->tail + 1) % $this->maxSize) == $this->head;
    }
}


$obj = new MyCircularQueue(8);
$ret_1 = $obj->enQueue(3);
$ret_2 = $obj->enQueue(9);
$ret_3 = $obj->enQueue(5);
$ret_4 = $obj->enQueue(0);
$ret_5 = $obj->Rear();
$ret_6 = $obj->isFull();
$ret_7 = $obj->deQueue();
$ret_8 = $obj->enQueue();
$ret_9 = $obj->Rear();
var_dump($ret_1,$ret_2,$ret_3,$ret_4,$ret_5,$ret_6,$ret_7,$ret_8,$ret_9);
